/*
 * Copyright 2008 Zoundry LLC, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.zoundry.gaedt.services.applications.starters;

import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.ui.DebugUITools;
import org.zoundry.gaedt.core.util.ZGaeUtil;
import org.zoundry.gaedt.services.applications.IZGaeApplication;
import org.zoundry.gaedt.services.applications.ZGaePythonApplication;


/**
 * Base class for application starters.
 */
public abstract class ZGaeApplicationStarterBase implements IZGaeApplicationStarter
{
    /** The application being started. */
    private ZGaePythonApplication mApplication;
    /** The launch configuration for this app starter. */
    private ILaunchConfiguration mLaunchConfiguration;

    /**
     * C'tor.
     *
     * @param aApplication
     */
    public ZGaeApplicationStarterBase(ZGaePythonApplication aApplication)
    {
        setApplication(aApplication);
    }

    /**
     * @see org.zoundry.gaedt.services.applications.starters.IZGaeApplicationStarter#start()
     */
    public void start()
    {
        doStart(ILaunchManager.RUN_MODE);
    }

    /**
     * @see org.zoundry.gaedt.services.applications.starters.IZGaeApplicationStarter#startDebug()
     */
    public void startDebug()
    {
        doStart(ILaunchManager.DEBUG_MODE);
    }

    /**
     * Do the start work - for both normal and debug modes.
     *
     * @param aRunMode
     */
    protected void doStart(String aRunMode)
    {
        try
        {
            // Get the launch config to use
            ILaunchConfiguration launchConfig = createLaunchConfiguration();

            // Launch the application
            DebugUITools.launch(launchConfig, aRunMode);

            // Save the launch config to reuse next time
            setLaunchConfiguration(launchConfig);
        }
        catch (Exception e)
        {
            ZGaeUtil.logAndDisplayError(e);
        }
    }

    /**
     * @see org.zoundry.gaedt.services.applications.starters.IZGaeApplicationStarter#stop()
     */
    public void stop()
    {
        ILaunch[] launches = DebugPlugin.getDefault().getLaunchManager().getLaunches();
        for (ILaunch launch : launches)
        {
            if (matches(launch))
            {
                try
                {
                    launch.terminate();
                }
                catch (DebugException e)
                {
                    ZGaeUtil.logError(e);
                }
                return;
            }
        }
    }

    /**
     * Creates a launch configuration for the application.
     *
     * @throws Exception
     */
    protected abstract ILaunchConfiguration createLaunchConfiguration() throws Exception;

    /**
     * @see org.zoundry.gaedt.services.applications.starters.IZGaeApplicationStarter#matches(org.zoundry.gaedt.services.applications.IZGaeApplication)
     */
    public boolean matches(IZGaeApplication aApplication)
    {
        return getApplication().equals(aApplication);
    }

    /**
     * @see org.zoundry.gaedt.services.applications.starters.IZGaeApplicationStarter#matches(org.eclipse.debug.core.ILaunch)
     */
    public boolean matches(ILaunch aLaunch)
    {
        if (getLaunchConfiguration() != null)
            return aLaunch.getLaunchConfiguration().equals(getLaunchConfiguration());
        return false;
    }

    /**
     * @see org.zoundry.gaedt.services.applications.starters.IZGaeApplicationStarter#getApplication()
     */
    public ZGaePythonApplication getApplication()
    {
        return mApplication;
    }

    /**
     * @param aApplication the application to set
     */
    protected void setApplication(ZGaePythonApplication aApplication)
    {
        mApplication = aApplication;
    }

    /**
     * @return the launchConfiguration
     */
    protected ILaunchConfiguration getLaunchConfiguration()
    {
        return mLaunchConfiguration;
    }

    /**
     * @param aLaunchConfiguration the launchConfiguration to set
     */
    protected void setLaunchConfiguration(ILaunchConfiguration aLaunchConfiguration)
    {
        mLaunchConfiguration = aLaunchConfiguration;
    }
}
